package com.qingfeng.client.bean;

import java.lang.reflect.Type;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 订单处理延迟消息
 *
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/1/9 2:52 下午
 */
public class TradeQueueMsg implements Delayed {

    private Long tradeId;

    private Long expiredTime;


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expiredTime , TimeUnit.NANOSECONDS)
                - unit.convert(System.currentTimeMillis(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (this == o) {
            return 1;
        }
        if (o == null) {
            return -1;
        }
        long diff = this.getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);
        return diff < 0 ? -1 : (diff == 0 ? 0 : 1);
    }

    public Long getTradeId() {
        return tradeId;
    }

    public void setTradeId(Long tradeId) {
        this.tradeId = tradeId;
    }

    public Long getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(Long expiredTime) {
        this.expiredTime = expiredTime;
    }


    @Override
    public String toString() {
        return "TradeQueueMsg{" +
                "tradeId=" + tradeId +
                ", expiredTime=" + expiredTime +
                '}';
    }
}
